import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { ConfigService, ConfigModule } from '@nestjs/config';

import { DatabaseModule } from '@server/modules/database/module';
import { UserModelProvider, LoginUserModelProvider } from '@server/models';
import { AuthService } from './service';
import { JwtStrategy } from './strategy';

@Module({
  imports: [
    JwtModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          secret: config.get('jwt.secret'),
        };
      },
    }),
    DatabaseModule
  ],
  providers: [
    AuthService,
    JwtStrategy,
    LoginUserModelProvider
  ],
  exports: [
    AuthService,
    JwtStrategy
  ]
})
class AuthModule {}

const injectModules = [
  ConfigService,
  PassportModule.register({ defaultStrategy: 'jwt' }),
  JwtModule.registerAsync({
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: (config: ConfigService) => {
      return {
        secret: config.get('jwt.secret')
      };
    },
  }),
  AuthModule
];

export { AuthService, JwtStrategy, injectModules };
